package com.oracle.sbdb.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import com.oracle.sbdb.bo.BugInfoBo;
import com.oracle.sbdb.servlet.model.BugInformation;
import com.oracle.sbdb.util.JSONUtility;

/**
 * Servlet implementation class UpdateBug
 */
@WebServlet("/updateBug")
public class UpdateBug extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public UpdateBug() {
		super();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String updatedBug = request.getParameter("bug");
		String reponseContent  = updateBug(updatedBug);
		PrintWriter out = response.getWriter();
		out.write(reponseContent);
		out.flush();
		out.close();
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		JSONObject requestJson = JSONUtility.getRequestString(request);
		String updatedBug = requestJson.getString("bug");
		String reponseContent  = updateBug(updatedBug);
		PrintWriter out = response.getWriter();
		out.write(reponseContent);
		out.flush();
		out.close();
	}

	private String updateBug(String requestPara){
		boolean status = false;
		try{
			if (requestPara != null) {
				BugInformation bugInformation = JSONUtility.json2Bean(requestPara,
						BugInformation.class);
				if (bugInformation != null) {
					BugInfoBo.INSTANCE.updateBug(bugInformation);
					status = true;
				}
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		 
		JSONObject res = new JSONObject();
		res.put("Status", status ? "Success" : "Fail");
		return res.toString();
	}
}
